package com.example.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.RespStat;
import com.example.demo.bean.Account;
import com.example.demo.mapper.AccountMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class AccountService {

    @Autowired
    AccountMapper accountMapper;
    public Account findByLoginNameAndPassword(String loginName, String password) {

        QueryWrapper<Account> wrapper = new QueryWrapper<>();
        wrapper.eq("login_name",loginName);
        wrapper.eq("password",password);
        Account account = accountMapper.selectOne(wrapper);

        return account;
    }

    public List<Account> findAll() {
//         List<Account> accountList =accountMapper.selectList(null);
        List<Account> accountList =accountMapper.findAll();
        return  accountList;
    }

    public PageInfo<Account> findByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Account> accountList =accountMapper.findAll();
        PageInfo<Account> accountPageInfo = new PageInfo<>(accountList,5);
        return  accountPageInfo;
    }

    public RespStat deleteById(int id) {
        //1. 提示用户
        //2. 通过删除标记 数据永远删不掉 /update 只做增，而不是真改
        int key = accountMapper.deleteById(id);
        return key==1 ? RespStat.build(200):
                RespStat.build(500,"删除处错");

    }

}
